{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('/home/husein/ssd3/gpt4all/translated-gpt4all-code.json') as fopen:\n",
    "    code = json.load(fopen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'prompt': ['Give me a Python function for a buffer that is smaller than the requested size.',\n",
       "  '\"Berikan saya fungsi Python untuk pengepam yang lebih kecil daripada saiz yang diminta.\"'],\n",
       " 'response': [\"You can define a buffer with a smaller size using the Python built-in bytearray() or by creating a custom array with the array module.\\nFor example:\\n\\n```python\\nbuffer = bytearray(256)\\n``` or\\n```python\\nimport array\\nbuffer = array.array('i', [0] * 256)\\n```\",\n",
       "  \"Anda boleh menentukan buffer dengan saiz yang lebih kecil menggunakan bytearray() bawaan Python atau dengan membuat array khusus dengan modul array. Contohnya:\\n\\n```python\\nbuffer = bytearray(256)\\n``` atau\\n```python\\nimport array\\nbuffer = array.array('i', [0] * 256)\\n```\"],\n",
       " 'source': 'unified_chip2'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "template = {\n",
    "    \"description\": \"Template used by Alpaca-LoRA.\",\n",
    "    \"prompt_input\": \"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\\n\\n### Instruction:\\n{instruction}\\n\\n### Input:\\n{input}\\n\\n### Response:\\n\",\n",
    "    \"prompt_no_input\": \"Below is an instruction that describes a task. Write a response that appropriately completes the request.\\n\\n### Instruction:\\n{instruction}\\n\\n### Response:\\n\",\n",
    "    \"response_split\": \"### Response:\"    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████| 70107/70107 [00:00<00:00, 342942.33it/s]\n"
     ]
    }
   ],
   "source": [
    "data = []\n",
    "for d in tqdm(code):\n",
    "    try:\n",
    "        a = d['response'][1]\n",
    "        for p in d['prompt']:\n",
    "            res = template[\"prompt_no_input\"].format(instruction=p)\n",
    "            res = f\"{res}{a}\"\n",
    "            data.append(res)\n",
    "    \n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "140214"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('prepared-gpt4all-code.json', 'w') as fopen:\n",
    "    json.dump(data, fopen)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
